import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";

/**
 * template - 模板model
 * @since 2019-1-29
 * @version 0.0.1

 */
import React, { PureComponent } from 'react';
import { Table, Popconfirm } from 'antd';
import { createPagination, isTenantRoleLevel, tableScrollWidth } from 'utils/utils';
import intl from 'utils/intl';
import { enableRender, operatorRender } from 'utils/renderer';
import { CPAAS_IMP } from 'utils/config';
import notification from 'utils/notification';
import queryString from 'querystring';
import { downloadFile } from 'cpaas-front/lib/services/api';
import { openTab } from 'utils/menuTab'; // 监听导出错误时 postMessage 事件

window.addEventListener('message', function (e) {
  var _e$data = e.data,
      type = _e$data.type,
      message = _e$data.message;

  if (type && type === 'templateExportError') {
    notification.error({
      message: intl.get('cpaas.common.notification.export.error').d('导出异常'),
      description: message
    });
  }
});
/**
 * 消息模板数据展示列表
 * @extends {PureComponent} - React.PureComponent
 * @reactProps {Function} onChange - 分页查询
 * @reactProps {Boolean} loading - 数据加载完成标记
 * @reactProps {Array} dataSource - Table数据源
 * @reactProps {Object} pagination - 分页器
 * @reactProps {Number} pagination.current - 当前页码
 * @reactProps {Number} pagination.pageSize - 分页大小
 * @reactProps {Number} pagination.total - 数据总量
 * @return React.element
 */

var ListTable = /*#__PURE__*/function (_PureComponent) {
  _inherits(ListTable, _PureComponent);

  var _super = _createSuper(ListTable);

  function ListTable() {
    _classCallCheck(this, ListTable);

    return _super.apply(this, arguments);
  }

  _createClass(ListTable, [{
    key: "changeEdit",
    value:
    /**
     * 编辑
     * @param {object} record - 消息模板对象
     */
    function changeEdit(record) {
      this.props.editContent(record);
    }
    /**
     * 删除
     * @param {object} record - 消息模板对象
     */

  }, {
    key: "delHeader",
    value: function delHeader(record) {
      this.props.delContent(record);
    }
    /**
     * 导出
     * @param {object} record - 消息模板对象
     */

  }, {
    key: "exportExcel",
    value: function exportExcel(record) {
      var organizationId = this.props.organizationId;
      var requestUrl = "".concat(CPAAS_IMP, "/v1/").concat(isTenantRoleLevel() ? "".concat(organizationId, "/") : '', "template/").concat(record.templateCode, "/excel");

      if (record) {
        downloadFile({
          requestUrl: requestUrl,
          queryParams: [{
            name: 'tenantId',
            value: record.tenantId
          }]
        });
      }
    }
  }, {
    key: "exportCsv",
    value: function exportCsv(record) {
      var organizationId = this.props.organizationId;
      var requestUrl = "".concat(CPAAS_IMP, "/v1/").concat(isTenantRoleLevel() ? "".concat(organizationId, "/") : '', "template/").concat(record.templateCode, "/csv");

      if (record) {
        downloadFile({
          requestUrl: requestUrl,
          queryParams: [{
            name: 'tenantId',
            value: record.tenantId
          }]
        });
      }
    }
    /**
     * 导入
     * @param {*} record
     */

  }, {
    key: "changeImport",
    value: function changeImport(record) {
      // 跳转到导入界面
      openTab({
        key: "/cimp/commentImport/".concat(record.templateCode),
        title: 'cpaas.common.title.templateImport',
        search: queryString.stringify({
          title: 'cpaas.common.title.templateImport',
          action: 'cimp.commentImport.view.button.templateImport',
          tenantId: record.tenantId,
          prefixPatch: record.prefixPatch,
          templateType: record.templateType
        })
      });
    }
    /**
     * render
     * @returns React.element
     */

  }, {
    key: "render",
    value: function render() {
      var _this = this;

      var _this$props = this.props,
          loading = _this$props.loading,
          dataSource = _this$props.dataSource,
          pagination = _this$props.pagination,
          onChange = _this$props.onChange;

      var paginations = _objectSpread(_objectSpread({}, createPagination(dataSource)), pagination);

      var columns = [!isTenantRoleLevel() && {
        title: intl.get("cimp.template.model.template.tenantName").d('租户'),
        dataIndex: 'tenantName'
      }, {
        title: intl.get("cimp.template.model.template.templateName").d('模板名称'),
        dataIndex: 'templateName'
      }, {
        title: intl.get("cimp.template.model.template.templateCode").d('模板代码'),
        dataIndex: 'templateCode',
        width: 150
      }, {
        title: intl.get("cimp.template.model.template.templateType").d('模板类型'),
        dataIndex: 'templateTypeMeaning',
        width: 100
      }, {
        title: intl.get("cimp.template.model.template.templateFileName").d('自定义模板名称'),
        dataIndex: 'templateFileName',
        width: 150
      }, {
        title: intl.get("cimp.template.model.template.description").d('描述'),
        dataIndex: 'description'
      }, {
        title: intl.get("cpaas.common.status").d('状态'),
        dataIndex: 'enabledFlag',
        width: 100,
        render: enableRender
      }, {
        title: intl.get('cpaas.common.button.action').d('操作'),
        key: 'action',
        width: 210,
        fixed: 'right',
        render: function render(_, record) {
          var operators = [{
            key: 'edit',
            ele: /*#__PURE__*/React.createElement("a", {
              onClick: function onClick() {
                _this.changeEdit(record);
              }
            }, intl.get('cpaas.common.button.edit').d('编辑')),
            len: 2,
            title: intl.get('cpaas.common.button.edit').d('编辑')
          }, {
            key: 'delete',
            ele: /*#__PURE__*/React.createElement(Popconfirm, {
              title: intl.get("cimp.template.view.message.title.confirmDelete").d('确定删除？'),
              onConfirm: function onConfirm() {
                return _this.delHeader(record);
              }
            }, /*#__PURE__*/React.createElement("a", null, intl.get('cpaas.common.button.delete').d('删除'))),
            len: 2,
            title: intl.get('cpaas.common.button.delete').d('删除')
          }, {
            key: 'import',
            ele: /*#__PURE__*/React.createElement("a", {
              onClick: function onClick() {
                return _this.changeImport(record);
              }
            }, intl.get('cpaas.common.button.import').d('导入')),
            len: 2,
            title: intl.get('cpaas.common.button.import').d('导入')
          }, {
            key: 'exportExcel',
            ele: /*#__PURE__*/React.createElement("a", {
              onClick: function onClick() {
                return _this.exportExcel(record);
              }
            }, intl.get('cimp.template.view.message.title.EXCEL').d('EXCEL')),
            len: 3,
            title: intl.get('cimp.template.view.message.title.EXCEL').d('EXCEL')
          }, {
            key: 'exportCsv',
            ele: /*#__PURE__*/React.createElement("a", {
              onClick: function onClick() {
                return _this.exportCsv(record);
              }
            }, intl.get('cimp.template.view.message.title.CSV').d('CSV')),
            len: 2,
            title: intl.get('cimp.template.view.message.title.CSV').d('CSV')
          }];
          return operatorRender(operators, record, {
            limit: 4,
            label: intl.get('cpaas.common.button.export').d('导出')
          });
        }
      }].filter(Boolean);
      return /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        loading: loading,
        rowKey: "id",
        columns: columns,
        scroll: {
          x: tableScrollWidth(columns)
        },
        dataSource: dataSource,
        pagination: paginations,
        onChange: onChange
      });
    }
  }]);

  return ListTable;
}(PureComponent);

export { ListTable as default };